package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.service.MenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @Autowired
    private JedisPool jedisPool;

    //获取动态菜单
    @RequestMapping("/getDynamicMenu")
    public Result getDynamicMenu(){
        try {
            //先获取当前登录的用户的用户名
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if(user!=null){
                String username=user.getUsername();
                String data = jedisPool.getResource().hget("DynamicMenu", username);
                List<Menu> list=null;
                if(!StringUtils.isBlank(data)){//从redis中查到了动态菜单
//                    List<Object> objectList = JSON.parseArray(data);
                    list = JSON.parseArray(data, Menu.class);
//                    list=new ArrayList<>();
//                    for (Object o : objectList) {
////                        Map<String,Object> map= (Map<String, Object>) o;
//                        Menu menu= (Menu) o;
//                        list.add(menu);
//                    }
                    System.out.println("从redis中查到的动态菜单11111");
                }else{//没有从redis中查到动态菜单，从数据库中去查
                    list=menuService.getDynamicMenu(username);
                    //然后放入redis缓存中
                    jedisPool.getResource().hset("DynamicMenu", username,JSON.toJSON(list).toString());
                    System.out.println("从数据库中查到的动态菜单22222");
                }
                return new Result(true, MessageConstant.GET_MENU_SUCCESS,list);
            }else {
                return new Result(false,MessageConstant.GET_MENU_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }

    }
	@RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Menu> menuList=menuService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    //查询关联
    @RequestMapping("/findmenuIdsByroleId")
    public Result findmenuIdsByroleId(Integer id){
        try {
            List<Integer> menus = menuService.findMenuIdsByRoleId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = menuService.pageQuery(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    //新建菜单
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            menuService.deleteById(id);
        } catch (RuntimeException e) {
            return new Result(false,"此菜单已关联角色,不能删除");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
    }

}
